﻿Option Explicit On

Imports System.Data
Imports System.Linq
Imports System.Data.SqlClient
Imports System.Windows.Controls
Imports System.Windows.Data
'Imports WindowsForms = System.Windows.Forms


Public Class DataBinding

    Public Overloads Shared Sub BindListView(ByVal Data As DataTable, ByVal ListViewControl As ListView)
        Dim bind As New Binding()
        
        bind.Source = Data

        ListViewControl.SetBinding(ListView.ItemsSourceProperty, bind)
    End Sub

    Public Shared Sub BindComboBox(ByVal data As DataTable, ByVal ComboBoxControl As ComboBox)
        Dim bind As New Binding()
        bind.Source = data
        ComboBoxControl.SetBinding(ListView.ItemsSourceProperty, bind)
    End Sub

    'Public Overloads Shared Sub BindListView(ByVal Data As DataTable, ByRef ListViewControl As WindowsForms.ListView, Optional ByVal StripCRLF As Boolean = False)
    '    ' populate and databind listview control with data in supplied datatable

    '    Dim lvi As WindowsForms.ListViewItem
    '    Dim subi As WindowsForms.ListViewItem.ListViewSubItem
    '    Dim i As Integer
    '    Dim myRow As DataRow

    '    Try

    '        ListViewControl.Items.Clear()


    '        For Each myRow In Data.Rows

    '            lvi = New WindowsForms.ListViewItem

    '            If Not IsDBNull(myRow.Item(0)) Then
    '                lvi.Text = myRow.Item(0)
    '            End If

    '            For i = 1 To myRow.Table.Columns.Count - 1

    '                subi = New WindowsForms.ListViewItem.ListViewSubItem

    '                If Not IsDBNull(myRow.Item(i)) Then
    '                    If Not StripCRLF Then
    '                        subi.Text = myRow.Item(i)
    '                    Else
    '                        Dim s As String = myRow.Item(i)

    '                        s = s.Replace(Chr(13), "").Replace(Chr(10), "")
    '                        subi.Text = s

    '                    End If
    '                End If

    '                lvi.SubItems.Add(subi)

    '            Next

    '            ListViewControl.Items.Add(lvi)

    '        Next

    '        Data.Dispose()

    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Critical)
    '    End Try


    'End Sub


    'Public Overloads Sub BindListView1(ByVal SQL As String, ByRef ListViewControl As ListView, Optional ByVal StripCRLF As Boolean = False)
    '    ' populate and databind listview control with data returned by SQL string
    '    Dim db As New CRM.DataLayer.DataAccess
    '    Dim itemtable As New DataTable

    '    Dim SQLReader As SqlDataReader
    '    Try
    '        SQLReader = db.GetSQLDataReader(SQL)
    '        Dim lvi As ListViewItem
    '        Dim subi As ListViewItem.ListViewSubItem
    '        Dim i As Integer

    '        ListViewControl.Items.Clear()

    '        Do
    '            While SQLReader.Read
    '                itemtable.Rows.Add()
    '                lvi = New ListViewItem


    '                If Not IsDBNull(SQLReader.Item(0)) Then
    '                    lvi.Content = SQLReader.Item(0)
    '                End If

    '                For i = 1 To SQLReader.FieldCount - 1

    '                    subi = New ListViewItem.ListViewSubItem

    '                    If Not IsDBNull(SQLReader.Item(i)) Then
    '                        If Not StripCRLF Then
    '                            subi.content = SQLReader.Item(i)
    '                        Else
    '                            Dim s As String = SQLReader.Item(i)

    '                            s = s.Replace(Chr(13), "").Replace(Chr(10), "")
    '                            subi.Text = s

    '                        End If
    '                    End If

    '                    lvi.SubItems.Add(subi)

    '                Next

    '                ListViewControl.Items.Add(lvi)

    '            End While

    '        Loop While SQLReader.NextResult

    '        SQLReader.Close()
    '        'DB.CloseDB()

    '    Catch ex As Exception
    '        MsgBox(ex.Message, MsgBoxStyle.Critical)
    '    Finally
    '        SQLReader = Nothing
    '    End Try


    'End Sub

    'Public Shared Sub SelectListviewRow(ByVal AnyList As Windows.Forms.ListView, ByVal ListId As Long)

    '    Dim li As Windows.Forms.ListViewItem

    '    AnyList.SelectedItems.Clear()

    '    For Each li In AnyList.Items
    '        If li.SubItems(0).Text = ListId Then
    '            li.Selected = True
    '            Exit For
    '        End If
    '    Next

    '    AnyList.Update()

    'End Sub
    'Public Shared Sub SelectListviewRow(ByVal AnyList As ListView, ByVal ListId As Long)

    '    Dim li As ListViewItem

    '    AnyList.SelectedItems.Clear()
    '    Dim currentRow As DataRowView = CType(, DataRowView)
    '    For Each li In AnyList.Items
    '        currentRow = CType(li, DataRowView)
    '        'If li = ListId Then
    '        '    li.Selected = True
    '        '    Exit For
    '        'End If
    '    Next

    '    AnyList.UpdateLayout()

    'End Sub

    Public Class MyListItem
        Public ItemText As String
        Public ItemValue As Long
    End Class

End Class
